package com.sirui.x.storage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sirui.x.storage.entity.StorageInfoEntity;
import com.sirui.x.storage.mapper.StorageInfoMapper;

@RestController
@RequestMapping("storage")
public class StorageController {

	@Autowired
	private StorageInfoMapper storageInfoMapper;

	@RequestMapping("test")
	public String test() {

		return "ok";
	}

	@RequestMapping("deduct")
	public int deduct(String commodityCode, int count) {
		StorageInfoEntity exist = storageInfoMapper.selectOne(
				Wrappers.<StorageInfoEntity>lambdaQuery().eq(StorageInfoEntity::getCommodityCode, commodityCode));

		if (exist == null || exist.getCount() < count) {
			throw new RuntimeException("库存不足");
		}
		StorageInfoEntity entity = new StorageInfoEntity();
		entity.setId(exist.getId());
		entity.setCount(exist.getCount() - count);
		return storageInfoMapper.updateById(entity);

	}
}
